/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package smary.ejb.stateless.auto;

import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceException;
import smary.ejb.entity.Userloginaccount;
import smary.ejb.stateless.EncryptionUnitLocal;

/**
 *Facade Klasse für die Userloginaccount Entität für allgemeine Datenbankoperationen.
 *Erweitert AbstractFacade. 
 * @author ACANPOWER
 */
@Stateless
public class UserloginaccountFacade extends AbstractFacade<Userloginaccount> {
    @PersistenceContext(unitName = "ScrummaryOnePU")
    private EntityManager em;
    
    @EJB
    EncryptionUnitLocal eul;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public UserloginaccountFacade() {
        super(Userloginaccount.class);
    }
    public Userloginaccount checkLoginData(String loginname, String password) {
        password = eul.stringToMD5(password);
        List<Userloginaccount> userlist = em.createQuery("SELECT u FROM Userloginaccount u WHERE u.passwort = '" + password + "' AND u.email = '" + loginname + "'", Userloginaccount.class).getResultList();
        if(userlist.size()> 1)throw new PersistenceException("More than one User found for specified Email and Password");
        if(userlist.size()>0)return userlist.get(0);
        return null;
    }

    @Override
    public void create(Userloginaccount entity) {
        entity.setPasswort(eul.stringToMD5(entity.getPasswort()));
        super.create(entity);
    }
    
}
